﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ARCam3D.Imaging.Processing
{
    public class GrayscaleImageConverter
    {

        unsafe public GrayscaleImage Convert(RGBImage image)
        {
            GrayscaleImage result = new GrayscaleImage(image.Width, image.Height);

            int bytesPerPixel = RGBImage.BytesPerPixel;
            int redOffset = RGBImage.RedOffset;
            int greenOffset = RGBImage.GreenOffset;
            int blueOffset = RGBImage.BlueOffset;

            long pointsCount = image.Width * image.Height;
            byte* imageDataPointer = (byte*)image.DataPtr.ToPointer();
            byte* resultDataPointer = (byte*)result.DataPtr.ToPointer();
            for (long point = 0; point < pointsCount; 
                ++point, imageDataPointer += bytesPerPixel, ++resultDataPointer)
            {
                *resultDataPointer = (byte)((imageDataPointer[redOffset] + imageDataPointer[greenOffset] + imageDataPointer[blueOffset]) / 3);
            }

            return result;
        }
    }
}
